/**
 * 核心类库
 */
var t = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (t) {
  return typeof t
}
  : function (t) {
    return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t
  },
  e = require("check");
module.exports = {
  toQueryPair: function (t, e) {
    return void 0 === e ? t : t + "=" + encodeURIComponent(null === e ? "" : String(e))
  },
  /*
  *处理请求路径
  * @param n 方法名
  * @param o 参数
  * return  string
  */
  getUrl: function (n, o) {
   var cont="",
   n = n.replace(/\//gi, "/");
   var a = getApp().globalData.api + "api/"+ n;
   return o && ("object" == (void 0 === o ? "undefined" : t(o)) ? a += "?" + e.param(o) : "string" == typeof o && (a += "&" + o)),
      a
  },
    /*
     *处理请求路径格式
     * @param t:缓存值
     * @param n:data参数
     * @param o:success后返回值
     * @param a:请求类型
     * return  string
     */
  json: function (t, n, o, i, a, c) {
    var r = getApp(),
      s = r.getCache("userinfo"),
     d = r.requirejs("core");
    i && d.loading()
    var p = a ? this.getUrl(t) : this.getUrl(t, n),
      l = {
        url: p,
        method: a ? "POST" : "GET",
        header: {
          "Content-type": a ? "application/x-www-form-urlencoded" : "application/json",
        }
      };
    a && (l.data = e.param(n)),//参数
      o && (l.success = function (t) {
        i && d.hideLoading();
        if (t.errMsg == "request:ok"){
          if (typeof (t.data) == "string") {
            if (t.data.indexOf("html") >= 0 && t.data.indexOf("head") >= 0 && t.data.indexOf("body") >= 0) {
              d.error('服务器错误！');
              return;
            }
          }
          "function" == typeof o, o(t.data)
        }else{
          this.alert(t.errMsg)
        }
         
      }),
      wx.request(l)
     console.log(l)
  },
  /*
  *post请求
  * @param n 方法名
  * return  string
  */
  post: function (t, e, n, o, i) {
    this.json(t, e, n, o, true, i)
  },
   /*
  *get请求
  * @param n 方法名
  * return  string
  */
  get: function (t, e, n, o, i) {
    this.json(t, e, n, o, false, i)
  },

  alert: function (e, n) {
    "object" === (void 0 === e ? "undefined" : t(e)) && (e = JSON.stringify(e)),
      wx.showModal({
        title: "提示",
        content: e,
        showCancel: false,
        success: function (t) {
          t.confirm && "function" == typeof n && n()
        }
      })
  },
  confirm: function (e, n, o) {
    "object" === (void 0 === e ? "undefined" : t(e)) && (e = JSON.stringify(e)),
      wx.showModal({
        title: "提示",
        content: e,
        showCancel: true,
        success: function (t) {
          t.confirm ? "function" == typeof n && n() : "function" == typeof o && o()
        }
      })
  },
  loading: function (t) {
    void 0 !== t && "" != t || (t = "加载中"),
      wx.showToast({
        title: t,
        icon: "loading",
        duration: 5e6
      })
  },
  hideLoading: function () {
    wx.hideToast()
  },
  toast: function (t, e) {
    e || (e = "loading"),
      wx.showToast({
        title: t,
        icon: e,
        duration: 1000
      })
  },
  error: function (t) {
    wx.showToast({
      title: t,
      icon: 'success',
      image: "/static/images/x.png",
      duration: 2000
    })
  },
  success: function (t) {
    wx.showToast({
      title: t,
      icon: "success",
      duration: 1000
    })
  },
  //event.currentTarget返回绑定事件的元素
  pdata: function (t) {
    return t.currentTarget.dataset
  },
  //event.target返回触发事件的元素
  data: function (t) {
    return t.target.dataset
  },
  phone: function (t) {
    var e = this.pdata(t).phone;
    wx.makePhoneCall({
      phoneNumber: e
    })
  },
  /**
   * 时间戳转时间字符串
   */
  time_format:function(time){
    var timestamp = time;
    var d = new Date(timestamp * 1000);    //根据时间戳生成的时间对象
    var date = (d.getFullYear()) + "-" +
      (d.getMonth() + 1) + "-" +
      (d.getDate()) + " " +
      (d.getHours()) + ":" +
      (d.getMinutes()) + ":" +
      (d.getSeconds());
    return date;
  },
  /**
   * 时间字符串转时间戳
   */
  time_str: function (date) {
    date = date.substring(0, 19);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime();
    return timestamp;
  },
  /**
 * json字符串转对象
 */
  json_parse: function (str) {
    var jsonStr = str;
    jsonStr = jsonStr.replace(" ", "");
    if (typeof jsonStr != 'object') {
      jsonStr = jsonStr.replace(/\ufeff/g, "");//重点
      var obj = JSON.parse(jsonStr);
      return obj
    }
  },
  /**
  * 对象转json字符串
  */
  str: function (obj) {
    var aToStr = JSON.stringify(obj);
    return aToStr;
  },
  /**
   * 腾讯地图
   */
  tx_map: function (lat, lng,name){
    lat = parseFloat(lat);
    lng = parseFloat(lng);
    var that=this,
    cache = getApp().getCache("map");
    if (cache) {
      wx.openLocation({
        latitude: lat,
        longitude: lng,
        scale: 28,
        name: name
      })
    } else {
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          console.log(res)
          getApp().setCache("map", res, 1200);
          var latitude = res.latitude
          var longitude = res.longitude
          wx.openLocation({
            latitude: lat,
            longitude: lng,
            scale: 28,
            name: name
          })
        },
        fail: function () {
          that.alert('未授权位置信息')
        }
      })
    }
  },
  /**
   * 复制到剪贴板
   */
  Clipboard:function(data){
    var that=this;
    wx.setClipboardData({
      data: data,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
           that.alert('成功复制到剪贴板')
          }
        })
      }
    })
  },
  ReName:function(name){
    wx.setNavigationBarTitle({
      title: name || "壹佰小程序"
    })
  },
  //跳转
  jump: function (url, i) {
    (!i || i == '') ? i = 1 : i = i;
    if (i == 1) {
      wx.navigateTo({
        url: url
      })
    } else if (i == 2) {
      wx.redirectTo({
        url: url
      })
    } else if (i == 3) {
      wx.reLaunch({//关闭所有页面，打开到应用内的某个页面。
        url: url
      })
    } else if (i == 4) {
      wx.switchTab({//跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面
        url: url
      })
    } else if (i == 5) {
      wx.navigateBack()
    }
  },
  /**
   * 判断元素是否在数组内
   */
  contains: function (arr, obj) {
    var i = arr.length;
    while (i--) {
      if (arr[i] == obj) {
        return true;
      }
    }
    return false;
  },
  /**
  * 图片预览
  * url str 当前图片路径
  * arr json字符串
  * field arr中图片路径的字段名
  */
  previewImage: function (url, arr, field) {
    var urls = [];

    if (arr && feild) {
      arr = JSON.parse(arr);
      arr.forEach(function (t, k) {
        urls[k] = t[field];
      });
    } else {
      urls = [url];
    }

    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: urls// 需要预览的图片http链接列表
    })
  },
}